//
//  ViewController.m
//  scanLineView
//
//  Created by 曾贤林 on 16/8/4.
//  Copyright © 2016年 rocky. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
{
    NSInteger dim;
    UIView *lineView;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSInteger screenWidth=300;
    NSInteger screenHeight=300;
    dim=300;
    
    UIView *polygonView = [[UIView alloc] initWithFrame: CGRectMake  ( (screenWidth/2) - (dim/2), (screenHeight/2) - (dim/2), dim, dim)];
    polygonView.center = self.view.center;
    //
    polygonView.layer.borderColor = [UIColor greenColor].CGColor;
    polygonView.layer.borderWidth = 3.0f;
    
    lineView= [[UIView alloc] initWithFrame:CGRectMake(0,0, dim, 2)];
    lineView.backgroundColor = [UIColor redColor];
    [polygonView addSubview:lineView];
    
    [self.view addSubview:polygonView];
    [self animationLineView];
    //
    CGRect bgRect = self.view.frame;
    UIImage *img =[self drawImage:bgRect AndClearRect:polygonView.frame];
    UIImageView*imgView = [[UIImageView alloc]initWithImage:img];
    imgView.frame=self.view.frame;
    [self.view addSubview:imgView];
}
-(UIImage *)drawImage:(CGRect)BgRect AndClearRect:(CGRect)ClearRect
{
    CGSize screenSize =[UIScreen mainScreen].bounds.size;
    UIGraphicsBeginImageContext(BgRect.size);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSetRGBFillColor(ctx, 0,0,0,0.5);
    CGRect drawRect =CGRectMake(0, 0, screenSize.width,screenSize.height);
    CGContextFillRect(ctx, drawRect);
    CGContextClearRect(ctx, ClearRect);
    UIImage* returnimage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return returnimage;
}

- (void)animationLineView
{
    [UIView animateWithDuration:5 delay:0 options: UIViewAnimationOptionRepeat  animations: ^{
        CGRect rect = lineView.frame;
        rect.origin.y = dim-3;
        lineView.frame = rect;
    }completion:nil];
  
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
